package com.nhc.model;

import android.os.Build;

import com.astrob.NavFrameSDK;
import com.astrob.util.SysUtils;
import com.astrob.util.Utils;
import com.nhc.database.dbmodel.SystemAllInfo;
import com.nhc.database.manager.SystemCenter;

import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;

import zuo.biao.library.util.MD5Util;

public class VCodeFileHandle {

    private static VCodeFileHandle instance = new VCodeFileHandle();

    public static VCodeFileHandle getInstance() {
        return instance;
    }

    public VCodeFileHandle() {
        reloadRightState();
    }

    private static final String PROPERTY_BUILD_VERSION_CUSTOM = "ro.custom.build.version";

    private String getProperty(String key, String defaultValue) {
        String value = defaultValue;
        try {
            Class<?> c = Class.forName("android.os.SystemProperties");
            Method get = c.getMethod("get", String.class, String.class);
            value = (String) (get.invoke(c, key, "unknown"));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return value;
        }
    }

//系统版本号
//String version =Build.DISPLAY

    //16位
    public String getDeviceID() {
        String code = getMacCode();
        if (code.length() < 1) {
            if (SysUtils.getInstance().is240x320) {
//                code = getProperty(PROPERTY_BUILD_VERSION_CUSTOM, "UNKNOW");
                code = Build.SERIAL; //FY2 不保存
            } else {
                code = Utils.getLocalMacAddress();//一般手机保存机器码
            }
            code = code.replace(":", "");
//            int size = code.length();
//            if (size < 16) {
//                for (int i = size; i < 16; i++) {
//                    code += "0";
//                }
//            }
            setMacCode(code);
        }
        if (code == null) {
            code = "";
        }
        return code;
    }

    //软件授权状态
    private int rightState = -13;

    public int getRightState() {
        return rightState;
    }

    //-2--无设备id -1--无试用期  0--试用期内  1--授权版本
    public int reloadRightState() {
        if (rightState == 1 || rightState == -2) {
            return rightState;
        }

        rightState = -1;
        String code = getDeviceID();
        if (code.length() < 1) {
            rightState = -2;
            return rightState;
        }
        boolean isRight = false;
        //文件校验
        int size = code.length();
        String strA = "";
        if (size < 16) {
            for (int i = size; i < 16; i++) {
                strA += "0";
            }
        }
        isRight = NavFrameSDK.getInstance().IsRightUser(code + strA, "");

        if (!isRight) {
            String licenseKey = getLicenseKey();
            isRight = checkVersion(code + strA, licenseKey);//机器码补足16位
        }
        if (!isRight) {
            if (isInforce()) {
                rightState = 0;
            }
        } else {
            rightState = 1;
        }

        return rightState;
    }

    private static final String MacCode = "MacCode";

    //设备码不保存
    private String getMacCode() {
        if (!SysUtils.getInstance().is240x320) {
            SystemAllInfo sInfo = SystemCenter.getInstance().getSysInfo(MacCode);
            if (sInfo == null) {
                return "";
            }
            return sInfo.getContent();
        }
        return "";
    }

    private void setMacCode(String code) {
        if (code == null) {
            return;
        }
        if (!SysUtils.getInstance().is240x320) {
            SystemAllInfo sInfo = SystemCenter.getInstance().getSysInfo(MacCode);
            if (sInfo == null) {
                sInfo = new SystemAllInfo(null, MacCode, code);
            } else {
                sInfo.setContent(code);
            }
            SystemCenter.getInstance().saveSysInfo(sInfo);
        }
    }

    private static final String USETIME = "firstuse";

    //是否在有效期内
    private boolean isInforce() {
        long firstusetime = 0;
        SystemAllInfo sInfo = SystemCenter.getInstance().getSysInfo(USETIME);
        if (sInfo != null) {
            firstusetime = Long.valueOf(sInfo.getContent());
        }

        if (firstusetime == 0) {
            firstusetime = System.currentTimeMillis();
            if (sInfo == null) {
                sInfo = new SystemAllInfo(null, USETIME, firstusetime + "");
            } else {
                sInfo.setContent(firstusetime + "");
            }
            SystemCenter.getInstance().saveSysInfo(sInfo);
        } else {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            long t = System.currentTimeMillis();
            String s1 = Utils.DateConverToString(new Date(t), df);
            String s0 = Utils.DateConverToString(new Date(firstusetime), df);
            long X = t - firstusetime;
            X = X / (1000 * 24 * 3600);
            if (X > 30) {
                return false;
            }
        }
        return true;
    }

    private static final String LICENSEKEY = "licensekey";
    //授权文件无需注册码
    public String getLicenseKey() {
        SystemAllInfo sInfo = SystemCenter.getInstance().getSysInfo(LICENSEKEY);
        if (sInfo == null) {
            return "";
        }
        return sInfo.getContent();
    }

    public void setLicenseKey(String licenseKey) {
        if (licenseKey == null) {
            licenseKey = "";
        }
        SystemAllInfo sInfo = SystemCenter.getInstance().getSysInfo(LICENSEKEY);
        if (sInfo == null) {
            sInfo = new SystemAllInfo(null, LICENSEKEY, licenseKey);
        } else {
            sInfo.setContent(licenseKey);
        }
        SystemCenter.getInstance().saveSysInfo(sInfo);
    }

    private boolean checkVersion(String deviceid, String licensekey) {
        boolean isRight = false;
        //授权 deviceid+ colin600->md5, 组合第0，5，6，8，10，13位，位共6位数
        deviceid += "colin600";
        String md5 = MD5Util.MD5(deviceid);
        if (md5.length() < 15) {
            return false;
        }
        String code = "";
        int[] keyIndexs = {0, 5, 6, 8, 13, 23};
        for (int i = 0; i < keyIndexs.length; i++) {
            int index = keyIndexs[i];
            code += md5.substring(index, index + 1);
        }

        if (licensekey.compareToIgnoreCase(code) == 0) {
            isRight = true;
            setLicenseKey(licensekey);
        }
        return isRight;
    }


}